var WxAutoImage = require('../../js/wxAutoImageCal.js');
var Onenet = require('../../js/onenet.js');

var app = getApp();
var API_KEY = "HVczX3vGKw7YCcKrZ2CmlYlaSI4="


Page({
  data: {
    imgUrls: [
      '../../image/onenet.jpg',
      '../../image/tecent.jpg',
      '../../image/rtt.jpg'
    ],
    num:0,
    sign:0,
    temp:31,
    humi:35,
    light:78,
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1200,
    room_names: ["客厅","卧室"],
    devices: [],
    employId: '',
    songtuSwitch: 0,
    jiaoshuiSwitch: 0,
    inTimeSwitch: 0,
    bindDisabled:false,
  },

  onLoad: function(){
    var that=this;
    setInterval(function () {
      if(that.data.inTimeSwitch==1){
        that.getOnenetData();
      }
      //console.log("轮播请求1秒触发一次");
    }, 1000)    //代表1秒钟发送一次请求    
  },
  onReady: function(){
    var that = this;
    that.getOnenetData();
  },

  //发送命令到onenet上进行开关数据更改
  setSwitch: function(){
    var that = this;
    if(that.data.songtuSwitch==1){
      Onenet.post_switch(2);
    }
  else {
    Onenet.post_switch(1);
  }
  if (that.data.jiaoshuiSwitch == 1) {
     Onenet.post_switch(4);
  }
  else {
    Onenet.post_switch(3);
  }
},

  //实时刷新界面
  btn_change_inTime: function (){
    var that = this;
    if(that.data.inTimeSwitch==0){
      that.setData({
        inTimeSwitch: 1
      })
    }
    else{
      that.setData({
        inTimeSwitch: 0
      })
    }
  },
  //松土开关变动
  btn_change_Songtu: function () {
    var that = this;
    if (that.data.songtuSwitch == 0) {
      that.setData({
        songtuSwitch: 1
      })
    }
    else {
      that.setData({
        songtuSwitch: 0
      })
    }
    console.log("松土开关值");
    console.log(that.data.songtuSwitch);
    that.setSwitch();
  },
//浇水开关变动
  btn_change_Jiaoshui: function (){
    var that=this;
    if(that.data.jiaoshuiSwitch==0){
      that.setData({
        jiaoshuiSwitch: 1
      })
    }
    else{
      that.setData({
        jiaoshuiSwitch: 0
      })
    }
    console.log("浇水开关值");
    console.log(that.data.jiaoshuiSwitch);
    that.setSwitch();
  },
  onShow: function(){
    var that=this;
    //that.getOnenetData();
  },

  getOnenetData:function() {
    var that = this
    let deviceid = "608581212"
    let apikey = "HVczX3vGKw7YCcKrZ2CmlYlaSI4="
    wx.request({
        url: "https://api.heclouds.com/devices/" + deviceid + "/datastreams",
        method: 'GET',
        data: {
          temp: '',
          humi: '',
          light: '',
          Songtu: '',
          Jiaoshui: '',
        },
        header: {
          "content-type": 'application/x-www-form-urlencoded',
          "api-key": apikey
        },
        success(res) {
          if (res.statusCode === 200) {
            console.log(res)
            that.setData({
              temp: res.data.data[0].current_value,
              humi: res.data.data[1].current_value,
              light: res.data.data[2].current_value,
              songtu: res.data.data[3].current_value,
              jiaoshui: res.data.data[4].current_value
            })
          }
        },
        fail: function (res) {
          wx.showToast({ title: '系统错误' })

        },
        complete: function (res) {
          wx.hideLoading()
        }
    })
  }
})

